from database import db
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, backref, mapped_column, relationship


class Client(db.Model):
    __tablename__ = "client"
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(50), nullable=False)
    surname: Mapped[str] = mapped_column(String(50), nullable=False)
    credit_card: Mapped[str] = mapped_column(String(50), nullable=False)
    car_number: Mapped[str] = mapped_column(String(10), nullable=False)


class Parking(db.Model):
    __tablename__ = "parking"
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    address: Mapped[str] = mapped_column(String(100), nullable=False)
    opened: Mapped[bool] = mapped_column(Boolean)
    count_places: Mapped[int] = mapped_column(Integer, nullable=False)
    count_available_places: Mapped[int] = mapped_column(Integer, nullable=False)


class ClientParking(db.Model):
    __tablename__ = "client_parking"
    __table_args__ = (
        UniqueConstraint("client_id", "parking_id", name="unique_client_parking"),
    )
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    client_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("client.id"), nullable=False
    )
    client = relationship(
        "Client", backref=backref("client_parkings", cascade="all, delete-orphan")
    )
    parking_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("parking.id"), nullable=False
    )
    parking = relationship(
        "Parking", backref=backref("client_parkings", cascade="all, delete-orphan")
    )

    time_in: Mapped[str] = mapped_column(DateTime)
    time_out: Mapped[str] = mapped_column(DateTime, nullable=True)
